﻿using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface.SheepFileModelInterface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Impl.SheepFileModellmpl
{
    public class SheepFileModelRepository : ISheepFileModelRepository
    {
        private readonly AgricultureDbContext _context;
        public SheepFileModelRepository(AgricultureDbContext context)
        {
            _context = context;
        }
        /// <summary>
        /// 羊只档案显示
        /// </summary>
        /// <returns></returns>
        public async Task<IQueryable<Sheepfilemodel>> GetSheepfilemodels()
        {
            return _context.Sheepfilemodels.Where(c => c.IsDel == false).AsNoTracking();
        }
        /// <summary>
        /// 档案添加
        /// </summary>
        /// <param name="sheepfilemodel"></param>
        /// <returns></returns>
        public async Task<int> AddSheepFileModel(Sheepfilemodel sheepfilemodel)
        {
            
            _context.Sheepfilemodels.Add(sheepfilemodel);
            return await _context.SaveChangesAsync();

        }
        /// <summary>
        /// 档案删除
        /// </summary>
        /// <param name="sheepfileId"></param>
        /// <returns></returns>
        public async Task<int> DeleteSheepFileModel(int sheepfileId)
        {
            var list = _context.Sheepfilemodels.FirstOrDefault(c=>c.SheepFileId == sheepfileId);
            if (list != null)
            {
                list.IsDel = true;
            }
            return await _context.SaveChangesAsync();
        }
        /// <summary>
        /// 档案反填
        /// </summary>
        /// <param name="sheepfileId"></param>
        /// <returns></returns>
        public async Task<Sheepfilemodel> ListSheepFileModel(int sheepfileId)
        {
            var list = _context.Sheepfilemodels.FirstOrDefault(c => c.SheepFileId == sheepfileId);
            return list;
        }
        /// <summary>
        /// 档案修改
        /// </summary>
        /// <param name="sheepfilemodel"></param>
        /// <returns></returns>
        public async Task<int> UpdaSheepFileModel(Sheepfilemodel sheepfilemodel)
        {
            _context.Sheepfilemodels.Update(sheepfilemodel);
            return await _context.SaveChangesAsync();
        }
		/// <summary>
		/// 批量添加档案
		/// </summary>
		/// <param name="sheepfilemodel"></param>
		/// <returns></returns>
		public async Task<int> AddListSheepFileModel(List<Sheepfilemodel> sheepfilemodel)
        {
            _context.Sheepfilemodels.AddRange(sheepfilemodel);
            return await _context.SaveChangesAsync();
        }
        /// <summary>
        /// 档案批量修改
        /// </summary>
        /// <param name="sheepfilemodel"></param>
        /// <returns></returns>
        public Task<int> UpdateListSheepFileModel(List<Sheepfilemodel> sheepfilemodel)
        {
            _context.Sheepfilemodels.UpdateRange(sheepfilemodel);
            return _context.SaveChangesAsync();
        }

        /// <summary>
		/// 根据耳号反填羊只档案
		/// </summary>
		/// <param name = "id" ></ param >
        /// < returns ></ returns >

        public async Task<Sheepfilemodel> DteilSheepfilemodel(string eartag)
        {
            var result = _context.Sheepfilemodels.Where(c => c.EarTag == eartag).FirstOrDefault();
            return result;
        }
    }
		

}

